<?php
	$theme='tundra';
	require_once('../../server.config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Hamilton Admin</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <!-- load the dojo toolkit base -->
        <style type="text/css">
            
            @import "<?php print(SS_DOJO_PATH) ?>dojo/resources/dojo.css";
            @import "<?php print(SS_DOJO_PATH) ?>dijit/themes/dijit.css";
            @import "<?php print(SS_DOJO_PATH) ?>dijit/themes/<?php print($theme) ?>/<?php print($theme) ?>.css";
            @import "<?php print(SS_DOJO_PATH) ?>dojox/grid/resources/Grid.css";
            @import "<?php print(SS_DOJO_PATH) ?>dojox/grid/resources/<?php print($theme) ?>Grid.css";
            @import "<?php print(SS_DOJO_PATH) ?>dojox/editor/plugins/resources/editorPlugins.css";
            @import "<?php print(SS_DOJO_PATH) ?>dojox/layout/resources/ExpandoPane.css";
            @import "/lib/dojoc/form/themes/TimestampTextBox.css";
            @import "<?php print(SS_DOJO_PATH) ?>dojox/form/resources/FileInput.css";
			@import "<?php print(SS_DOJO_PATH) ?>dojox/form/resources/CheckedMultiSelect.css";
			
			.slipstreamViewEntry .dijitTextBox, .slipstreamViewEntry .dijitComboBox {
                width: 280px;
                margin: 0
            } .slipstreamViewEntry .dijitTextBox.halfwidth {
                width: 140px
            }
            
            /*.dijitExpandoPane  { border: 1px solid #ccc}*/
            label {
                display: block;
                margin-top: 10px
            } .dijitTextBox {
                padding: 3px
            }
            
            html, body {
                width: 100%;
                height: 100%;
                margin: 0;
                padding: 0;
                overflow: hidden
            }
        </style>
        <script type="text/javascript">
            passthrough = function(msg){
                //for catching messages from Flash
                if (window.console) {
                    console.log(msg);
                }
            }
        </script>
		<!--<script type="text/javascript" src="../lib/dojo-1.3.0-src/dojo/dojo.js" djConfig="locale: 'se', isDebug: true, parseOnLoad: true, usePlainJson: true"></script>
        <script type="text/javascript">
            dojo.registerModulePath("dojoc", "../../dojoc");
            dojo.registerModulePath("slipstream", "../../slipstream");
        </script>
        <script type="text/javascript" src="/lib/slipstream/adminLayer.js"></script>-->
		<script type="text/javascript" src="<?php print(SS_DOJO_PATH) ?>dojo/dojo.js" djConfig="locale: 'se', isDebug: false, parseOnLoad: true, usePlainJson: true"></script>
        <script type="text/javascript">
            dojo.registerModulePath("dojoc", "../dojoc");
            dojo.registerModulePath("slipstream", "<?php print(str_repeat('../',substr_count(SS_DOJO_PATH,'/')-1)) ?>slipstream");
        </script>
        <script type="text/javascript" src="<?php print(SS_DOJO_PATH) ?>dojo/ss_admin.js"></script>
        <script type="text/javascript">
            /*dojo.require("dojox.form.FileUploader");
            dojo.require("dojox.editor.plugins.UploadImage");
			dojo.require("dijit.layout.ContentPane");
			dojo.require("dijit.layout.TabContainer");
			dojo.require("dojoc.form.TimestampTextBox");
			dojo.require("dojox.form.MultiComboBox");
			dojo.require("dijit.Editor");;
			dojo.require("dojox.data.ClientFilter");
			dojo.require("dojox.data.JsonRestStore");
			dojo.require("dijit.Tree");
			dojo.require("dijit._tree.dndSource");
			dojo.require("slipstream.form.CheckedMultiSelect");*/
			dojo.require("slipstream.view.MDEditor");
			
			
			dojo.extend(dijit.form.CheckBox, {
				checkedAsValue: true,
				_getValueAttr: function(){
					if(this.checkedAsValue) return this.attr('checked');
					this.inherited(arguments);
			}});
			
			dojo.extend(dojox.form.MultiComboBox, {
				_getValueAttr: function(){
					return this._lastValue.replace(/,\s$/g,'');
			}});
			
			dojo.extend(dojox.editor.plugins.UploadImage, {
				_initButton: function(){
					this.command = "uploadImage";
					this.editor.commands[this.command] = "Upload Image";
					this.inherited("_initButton", arguments);
					delete this.command;
					console.debug('Initiating button timeout',this.renderEvent);
					if(this.renderEvent){
						dojo.subscribe(this.renderEvent, dojo.hitch(this, "createFileInput"));
						//dojo.addOnLoad(dojo.hitch(this, "createFileInput"));
					} else {
						setTimeout(dojo.hitch(this, "createFileInput"), 200);
					}
				}
			});
			
			/*dojo.extend(dijit.layout.AccordionContainer, {
				startup: function(){
					if(this._started){ return; }
					this.inherited(arguments);	
					if(this.selectedChildWidget){
						var style = this.selectedChildWidget.containerNode.style;
						style.display = "";
						style.overflow = "hidden";
						this.selectedChildWidget._buttonWidget._setSelectedState(true);
					}
				}
			});*/
			
			function formatDate(inDatum){
				return (inDatum != dojox.grid.na ? dojo.date.locale.format(new Date(inDatum), this.constraint) : dojox.grid.na);;
			}
			
			function convertToSlug(str){
				str=str.replace(/å/gi,'a');
				str=str.replace(/ä/gi,'a');
				str=str.replace(/ö/gi,'o');
				str=str.replace(/è/gi,'e');
				str=str.replace(/é/gi,'e');
				str=str.split(/[^a-zA-Z0-9-]+/).join("_");
				return str.toLowerCase();
			}
			
			function setupStores(){
				console.debug('store setup');
				entryStore = new dojox.data.JsonRestStore({
					target:"/api/entry/",
					idAttribute:"identry",
					labelAttribute:"name",
					/*queryOptions: {cache:true},*/
					schema: {
						properties: {
							date_mod: {
								format: "date-time"
							},
							date_create: {
								format: "date-time"
							},
							date_pub: {
								format: "date-time"
							},
							date_rej: {
								format: "date-time"
							}
						}
					}
				});
				
				staffStore = new dojox.data.JsonRestStore({
					target:"/api/staff/",
					idAttribute:"idstaff",
					labelAttribute:"firstname",
					/*queryOptions: {cache:true},*/
					schema: {
						properties: {
							firstname: {
								'default': 'Förnamn',
							},
							lastname: {
								'default': 'Efternamn',
							},
							slug: {
								'default': 'fornamn_efternamn',
							}
						}
					},
					onSet:function(item,attribute,old,value){
						//this.save();
					}
				});
			
				categoryStore = new dojox.data.JsonRestStore({
					target:"/api/category/",
					idAttribute:"idcategory",
					labelAttribute:"name"
				});
			
				officeStore = new dojox.data.JsonRestStore({
					target:"/api/office/",
					idAttribute:"idoffice",
					labelAttribute:"name"
				});
			
				structureStore = new dojox.data.JsonRestStore({
					target:"/api/structure/",
					idAttribute:"idstructure",
					labelAttribute:"name"
				});
			
				areaStore = new dojox.data.JsonRestStore({
					target:"/api/businessarea/",
					idAttribute:"idbusinessarea",
					labelAttribute:"name",
					schema: {
						properties: {
							name: {
								'default': 'Namn',
							},
							slug: {
								'default': 'namn',
							}
						}
					},
					queryOptions: {cache:true},
				});
				dojo.connect(areaStore,'onNew',function(item,parent){console.debug('New item',item)});
				
			}
			
			function preloadStores(){
				console.debug('preload');
				categoryStore.fetch();
				//categoryStore.fetch({queryOptions: {cache:true}});
				structureStore.fetch({queryOptions: {cache:true}});
			}
			
			function init(){
				console.debug('init');
				
				structureTreeModel = new dijit.tree.TreeStoreModel({
			        store: structureStore,
			        query: {idstructure:1},
					labelAttr: 'name',
			        childrenAttrs: ["children"],
					mayHaveChildren: function(item){
						return item.children.length>0;
					}
					});
					
				var outerFrame=new dijit.layout.BorderContainer({
					style: 'width: 100%; height: 100%; border: 0; overflow: hidden'
				});
				var header=new dijit.layout.ContentPane({
					title:"Header",
					region: 'top',
					style: 'height: 20px; border: 0; margin: 0',
					content: 'header'
				});
				outerFrame.addChild(header);
				var tabContainer=new dijit.layout.TabContainer({
					title:"Middle",
					region: 'center',
					style: 'overflow: hidden'
				});
				outerFrame.addChild(tabContainer);
				/*var center=new dijit.layout.ContentPane({
					title:"Center",
					region: 'center'
				});
				outerFrame.addChild(center);*/
				var footer=new dijit.layout.ContentPane({
					title:"Footer",
					region: 'bottom',
					content: 'footer'
				});
				outerFrame.addChild(footer);
				var welcomeView=new dijit.layout.ContentPane({
					title:"Välkommen",
					style: 'overflow: hidden',
				});
				tabContainer.addChild(welcomeView);
				entryView=new slipstream.view.MDEditor({
					title: 'Inlägg',
					style: 'overflow: hidden',
					store: entryStore,
					structure: [
						{
							name: 'Namn',
							field: 'headline',
							width: '100px'
						},{
							name: 'Publish date',
							field: 'date_pub',
							width: 'auto',
							constraints: {datePattern:'yyyy-MM-dd'},
							formatter: formatDate,
							editor: dojoc.form.TimestampTextBox,
							editable: true
						}
					],
					templatePath: dojo.moduleUrl("slipstream.view", "templates/Entry.html"),
					attributeMap: {
						/*store: "grid",*/
						structure: "grid",
					}
				});
				tabContainer.addChild(entryView);
				staffView = new slipstream.view.MDEditor({
					title: 'Personal',
					style: 'overflow: hidden',
					templatePath: dojo.moduleUrl("slipstream.view", "templates/Staff.html"),
					store: staffStore,
					prefix: 'staff_',
					structure: [
						{
							name: 'Förnamn',
							field: 'firstname',
							width: '100px'
						},{
							name: 'Efternamn',
							field: 'lastname',
							width: 'auto'
						}
					],
					attributeMap: {
						/*store: "grid",*/
						structure: "grid",
					}
				});
				tabContainer.addChild(staffView);
				categoryView = new slipstream.view.MDEditor({
					title: 'Kategorier',
					style: 'overflow: hidden',
					templatePath: dojo.moduleUrl("slipstream.view", "templates/Category.html"),
					store: categoryStore,
					structure: [
						{
							name: 'Namn',
							field: 'name',
							width: 'auto'
						}
					],
					attributeMap: {
						/*store: "grid",*/
						structure: "grid",
					}
				});
				tabContainer.addChild(categoryView);
				officeView = new slipstream.view.MDEditor({
					title: 'Kontor',
					style: 'overflow: hidden',
					templatePath: dojo.moduleUrl("slipstream.view", "templates/Office.html"),
					store: officeStore,
					structure: [
						{
							name: 'Namn',
							field: 'name',
							width: 'auto'
						}
					],
					attributeMap: {
						/*store: "grid",*/
						structure: "grid",
					}
				});
				//tabContainer.addChild(officeView);
				areaView = new slipstream.view.MDEditor({
					title: 'Affärsområde',
					style: 'overflow: hidden',
					templatePath: dojo.moduleUrl("slipstream.view", "templates/Area.html"),
					store: areaStore,
					structure: [
						{
							name: 'Namn',
							field: 'name',
							width: 'auto'
						}
					],
					attributeMap: {
						/*store: "grid",*/
						structure: "grid",
					}
				});
				//tabContainer.addChild(areaView);
				structureView = new slipstream.view.MDEditor({
					title: 'Struktur',
					style: 'overflow: hidden',
					templatePath: dojo.moduleUrl("slipstream.view", "templates/Structure.html"),
				    store: structureStore,
					/*model: 'structureTreeModel',
					attributeMap: {
						model: "tree",
					}*/
				});
				tabContainer.addChild(structureView);
				dojo.body().appendChild(outerFrame.domNode);
				outerFrame.startup();
				dojo.publish('ss/renderDone');
				console.debug('end');
			}
			
			function test(){
				console.debug(dojo.body());
			}
			/*setupStores();
			preloadStores();
			storeStructure=[
				       { name: 'Namn', field: 'headline',width: '100px'},
				       { name: 'Publish date', field: 'date_pub', width: 'auto', constraints: {datePattern:'yyyy-MM-dd'}, formatter: formatDate, editor: dojoc.form.TimestampTextBox, editable: true}];*/
			dojo.addOnLoad(setupStores);
			dojo.addOnLoad(preloadStores);
			dojo.addOnLoad(init);
                        		
                    
        </script>
    </head>
    <body class="<?php print($theme) ?>">
    </body>
</html>
